
//根据经纬度计算距离
function getTheDisance(lat1, lng1, lat2, lng2) {
    var radLat1 = getRad(lat1);
    var radLat2 = getRad(lat2);

    var a = radLat1 - radLat2;
    var b = getRad(lng1) - getRad(lng2);

    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000.0;
    if ((s / 1000) > 1000) {
        s = ">999"
    } else {
        s = (s / 1000).toFixed(1)
    }
    if(s.indexOf('.')>0){
        if(s.split('.')[0].length>2){
            s=s.split('.')[0];
        }
    }
    return s+"km";

}
var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;
function getRad(d) {
    return d * PI / 180.0;
}

// 经纬度获取距离
// function getMyLocation(lat,lon){
//     var result="";
//     wx.getLocation({
//         type: 'wgs84',
//         success: function(res) {
//             // console.log(lat+"/"+lon+"/"+res.latitude+"/"+res.longitude)
//             result=  getTheDisance(lat,lon,res.latitude,res.longitude)
//             return result;
//         }
//     })
// }
// 经纬度获取距离
function getMyLocation(opts) {
    wx.getLocation({
        type: 'wgs84',
        success: function (res) {
            if(opts.callBack){
                opts.callBack(res.latitude,res.longitude)
            }
        }
    })
}

function test() {
    console.log("test")
}

module.exports.getMyLocation = getMyLocation;
module.exports.getTheDisance = getTheDisance;
module.exports.test = test;